﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;

namespace APMAWebConsole.TrackingPart
{
    public partial class TeamStatus : System.Web.UI.Page
    {
        private const string TeamStatusDataSourceKey = "TeamStatusDataSourceKey";
        private const string UsersListKey = "UsersListKey";
        private const string IterationsListKey = "IterationsListKey";
        private const string ReleasesListKey = "ReleasesListKey";

        private static Dictionary<string, object> parameters = new Dictionary<string, object>() {{"Owner_id", 0},{"Iteration_id", 0},{"Release_id", 0}};
        
        public DataTable DataSource
        {
            get
            {
                return ViewState[TeamStatusDataSourceKey] != null
                           ? (DataTable)ViewState[TeamStatusDataSourceKey]
                           : (DataTable)(ViewState[TeamStatusDataSourceKey] = SiteMaster.Proxy.GetTeamStatusList());
            }

            set { ViewState[TeamStatusDataSourceKey] = value; }
        }

        public DataTable UsersList
        {
            get
            {
                return ViewState[UsersListKey] != null
                           ? (DataTable)ViewState[UsersListKey]
                           : (DataTable)(ViewState[UsersListKey] = SiteMaster.Proxy.GetUsersList());
            }

            set { ViewState[TeamStatusDataSourceKey] = value; }
        }

        public DataTable IterationsList
        {
            get
            {
                return ViewState[IterationsListKey] != null
                           ? (DataTable)ViewState[IterationsListKey]
                           : (DataTable)(ViewState[IterationsListKey] = SiteMaster.Proxy.GetIterationsListId());
            }

            set { ViewState[TeamStatusDataSourceKey] = value; }
        }

        public DataTable ReleasesList
        {
            get
            {
                return ViewState[ReleasesListKey] != null
                           ? (DataTable)ViewState[ReleasesListKey]
                           : (DataTable)(ViewState[ReleasesListKey] = SiteMaster.Proxy.GetReleasesList());
            }

            set { ViewState[TeamStatusDataSourceKey] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.User.Identity.IsAuthenticated)
                Response.Redirect("~/Account/Login.aspx");
        }

        private void Rebind()
        {
            ddlUserFilter.DataValueField = "Id";
            ddlUserFilter.DataTextField = "Name";
            ddlUserFilter.DataSource = UsersList;
            ddlUserFilter.DataBind();
            ddlUserFilter.Items.Insert(0, new ListItem(String.Empty, String.Empty));
            ddlUserFilter.SelectedIndex = (int)parameters["Owner_id"];

            ddlIterationFilter.DataValueField = "Id";
            ddlIterationFilter.DataTextField = "Id";
            ddlIterationFilter.DataSource = IterationsList;
            ddlIterationFilter.DataBind();
            ddlIterationFilter.Items.Insert(0, new ListItem(String.Empty, String.Empty));
            ddlIterationFilter.SelectedIndex = (int)parameters["Iteration_id"];

            ddlReleaseFilter.DataValueField = "Id";
            ddlReleaseFilter.DataTextField = "Id";
            ddlReleaseFilter.DataSource = ReleasesList;
            ddlReleaseFilter.DataBind();
            ddlReleaseFilter.Items.Insert(0, new ListItem(String.Empty, String.Empty));
            ddlReleaseFilter.SelectedIndex = (int)parameters["Release_id"];

            grdTeamStatus.DataSource = DataSource;
            grdTeamStatus.DataBind();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            Rebind();
        }

        protected void OnFilterSelected(object sender, EventArgs e)
        {
            switch (((DropDownList)sender).ID)
            {
                case "ddlUserFilter":
                    {
                        if (((DropDownList)sender).SelectedIndex != 0)
                        {
                            SetFilter("Owner_id", (((DropDownList) sender).SelectedIndex));
                        }
                        else
                        {
                            RemoveFilter("Owner_id");
                        }
                        break;
                    }
                case "ddlIterationFilter":
                    {
                        if (((DropDownList)sender).SelectedIndex != 0)
                        {
                            SetFilter("Iteration_id", (((DropDownList)sender).SelectedIndex));
                        }
                        else
                        {
                            RemoveFilter("Iteration_id");
                        }
                        break;
                    }
                case "ddlReleaseFilter":
                    {
                        if (((DropDownList)sender).SelectedIndex != 0)
                        {
                            SetFilter("Release_id", (((DropDownList)sender).SelectedIndex));
                        }
                        else
                        {
                            RemoveFilter("Release_id");
                        }
                        break;
                    }
            }
        }

        private void SetFilter(string filterName, int filterId)
        {
            parameters[filterName] = filterId;
            
            DataSource = SiteMaster.Proxy.GetTeamStatusListWithFilter(parameters);
        }

        private void RemoveFilter(string filterName)
        {
            parameters[filterName] = 0;
            DataSource = SiteMaster.Proxy.GetTeamStatusListWithFilter(parameters);
        }
    }
}